﻿using System;
using Nancy;

namespace GeometryServer.Modules
{
    //http://localhost:13775/rest/services/Geometry/GeometryServer/lengths?sr=4269&polylines=[{"paths":[[[-117,34],[-116,34],[-117,33]],[[-115,44],[-114,43],[-115,43]]]},{"paths":[[[32.49,17.83],[31.96,17.59],[30.87,17.01],[30.11,16.86]]]}]&lengthUnit=9036&geodesic=true&f=json
    public class LengthsModule : NancyModule
    {
        public LengthsModule()
            : base("/rest/services/Geometry/GeometryServer/")
        {
            Get["/lengths"] = parameters =>
            {
                var model = new Models.Lengths();

                var exception = new GISServer.Core.Exception();
                exception.Error = new GISServer.Core.Error
                {
                    Code = 400,
                    Message = "Unable to complete areas and length operation."
                };


                #region Parameter

                if (Request.Query["f"].Value == "json")
                {
                    model.Format = Request.Query["f"].Value;
                }
                else
                {
                    model.Format = "HTML";
                }


                if (Request.Query["polylines"].Value != null)
                {
                    model.polylines = Request.Query["polylines"].Value;
                }

                if (Request.Query["sr"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["sr"].Value, out value);
                    if (successful)
                    {
                        model.SpatialReference = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'sr' parameter is invalid");
                    }
                }

                if (Request.Query["lengthUnit"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["lengthUnit"].Value, out value);
                    if (successful)
                    {
                        model.LengthUnit = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'lengthUnit' parameter is invalid");
                    }

                }

                if ((Request.Query["geodesic"].Value != null) && (Request.Query["geodesic"].Value == "true"))
                {
                    model.Geodesic = true;
                }
                else
                {
                    model.Geodesic = false;
                }


                #endregion

                var output_areas_lengths = new GISServer.Core.Client.GeometryService.Lengths();

                if ((model.polylines != null) && (!model.HasException))
                {
                    try
                    {
                        var inputpolyline = GISServer.Core.Client.Utilities.Parser.getServerPolylineArray(model.polylines);
                        var dotspatial_polyline_array = Services.Utilities.getDotSpatialPolyLineArray(inputpolyline);
                        output_areas_lengths = Services.Compute.Lengths(dotspatial_polyline_array);
                    }
                    catch (Exception)
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("Unable to calculate length the geometry");
                    }
                }

                if (!model.HasException)
                {
                    model.Result = output_areas_lengths.ToJson();
                }
                else
                {
                    model.Exception = exception.ToJson();
                }


                if (model.Format.Equals("json"))
                {
                    if (!model.HasException)
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Result);
                    }
                    else
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Exception);
                    }
                }
                else
                {
                    return View["Lengths", model];
                }
            };

        }
    }
}